Namespace persistent.Base.Customers

    Public Class Contract
        Implements ICustomerRepresenter


#Region "Instance Members"
        Private _Source As DataRow
        Private _Customer As Customer
        Private _CustomerStructure As CustomerStructures

        Private Sub New(ByVal contractID As Integer, Optional ByVal customer As Customer = Nothing)
            Me._Source = persistent.myDBConn.myORATable(String.Format("select coa.*, fn_coid_to_msisdn(coa.co_id) msisdn, ccs.ch_status from contract_all coa, curr_co_status ccs where ccs.co_id = coa.co_id and  coa.co_id = {0}", contractID)).Rows(0)

            Me._CustomerStructure = Asc(persistent.myDBConn.ORAScalar(String.Format("select tmtype from tarifftype where tmcode = {0}", Me._Source("TMCODE"))))
            If customer Is Nothing Then
                Me._Customer = customer.GetByCustomerID(CustomerContractInteractions.ConvertContractIDtoCustomerID(contractID))
            End If
        End Sub


        Public ReadOnly Property MSISDN() As String
            Get
                Return Me._Source("MSISDN")
            End Get
        End Property

        Public ReadOnly Property ContractID() As Integer Implements ICustomerRepresenter.ID
            Get
                Return Me._Source("CO_ID")
            End Get
        End Property

        Public ReadOnly Property Customer() As Customer ' Implements ICustomerRepresenter.Parent
            Get
                Return Me._Customer
            End Get
        End Property

        Public Function ToDataRow() As DataRow
            Return Me._Source
        End Function

#End Region


#Region "Static Members"

#Region "Existence Checks (static)"

        Public Shared Function CheckExistenceByMSISDN(ByVal msisdn As String) As Boolean
            Return persistent.Base.Customers.CustomerContractInteractions.ConvertMSISDNtoContractID(msisdn) > -1
        End Function

        Public Shared Function CheckExistenceByContractID(ByVal contractID As Integer) As Boolean
            Return persistent.Base.Customers.CustomerContractInteractions.ConvertContractIDtoCustomerID(contractID) > -1
        End Function

#End Region


#Region "Contract Retrival (static)"

        Public Shared Function GetByMSISDN(ByVal msisdn As String) As Contract
            If Not CheckExistenceByMSISDN(msisdn) Then
                Throw New Exception("No customer found by MSISDN: " & msisdn)
            End If

            Return GetByContractID(persistent.Base.Customers.CustomerContractInteractions.ConvertMSISDNtoContractID(msisdn))
        End Function

        Public Shared Function GetByContractID(ByVal contractID As Integer) As Contract
            Return GetByContractID(contractID, Nothing)
        End Function

        Public Shared Function GetByContractID(ByVal contractID As Integer, ByVal customer As Customer) As Contract
            If Not CheckExistenceByContractID(contractID) Then
                Throw New Exception("No customer found by ContractID: " & contractID)
            End If

            Return New Contract(contractID, customer)
        End Function

#End Region

#End Region

        Public ReadOnly Property Children() As ICustomerRepresenter() Implements ICustomerRepresenter.Children
            Get
                Return Nothing

                'If Me._Customer Is Nothing Then
                '    Return Nothing
                'Else
                '    Return Me._Customer.Children
                'End If
            End Get
        End Property

        Public ReadOnly Property CustomerStructure() As CustomerStructures Implements ICustomerRepresenter.CustomerStructure
            Get
                Return Me._CustomerStructure
            End Get
        End Property

        Public ReadOnly Property IsActive() As Boolean Implements ICustomerRepresenter.IsActive
            Get
                Return Me._Source("CH_STATUS") = "a"
            End Get
        End Property

        'Public ReadOnly Property ID() As Integer Implements ICustomerRepresenter.ID
        '    Get
        '        Return Me.ContractID
        '    End Get
        'End Property

        Public ReadOnly Property Parent() As ICustomerRepresenter Implements ICustomerRepresenter.Parent
            Get
                Return Me._Customer
            End Get
        End Property
    End Class

End Namespace